package cn.myweixin.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.ElementModifier;
import org.dom4j.io.SAXReader;


import com.thoughtworks.xstream.XStream;

import cn.myweixin.po.TextMessage;


public class MessageUtil {
  public static Map<String, String>  xmlToMap(HttpServletRequest request) throws DocumentException, IOException{
	  
	  Map< String, String> map= new HashMap<String,String>();
	  SAXReader reader=new SAXReader();
	  InputStream inputStream=request.getInputStream();
	  Document document=reader.read(inputStream);
	  
	  Element element=document.getRootElement();
	  
	  List<Element> list=element.elements();
	  for(Element e :list){
		  
		  map.put(e.getName(), e.getText());
		  
	  }
	  
	  inputStream.close();
      return map;
	  
  }
	
  /**
   * 将文本消息对象转化为XML
   * @param mess
   * @return
   */
  public static String textMessageToXml(TextMessage mess){
	  XStream xStream=new XStream();
	  xStream.alias("xml", mess.getClass());
	 return  xStream.toXML(mess);
	  
	  
  }
	
}
